package netty.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import netty.heartbeat.MyServerHandler;

import java.util.concurrent.TimeUnit;

/**
 * Created by yu756 on 2020/1/7 14:24
 */
public class MyServer {
    public static void main(String[] args) throws InterruptedException {


        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {

            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            // 基于http协议 使用http编码解码器
                            // 是以块方式写 添加chunkedWrite处理器
                            pipeline.addLast(new HttpServerCodec())
                                    .addLast(new ChunkedWriteHandler())
                                    /**
                                     *  http 数据在传输中是分段，HttpObjectAggregator将多个段聚合起来
                                     *  当浏览器发送大量数据的时候就会发送多次http请求
                                     */
                                    .addLast(new HttpObjectAggregator(8192))
                                    // 对于websocket 数据以frame帧 传递  可以webSocketFrame 下面六个子类
                                    // 浏览器请求格式   ws://localhost:7000/xxx
                                    // WebSocketServerProtocolHandler 核心功能将http协议升级为ws协议   保持长连接
                                    .addLast(new WebSocketServerProtocolHandler("/hello"))
                                    // 自定义handler 处理业务逻辑
                                    .addLast(new MyTestWebSocketFrameHandler());

                        }
                    });

            System.out.println("netty 启动");

            ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();

        }


    }


}
